package fun.sboy.view;

import fun.sboy.Main;
import fun.sboy.listener.MouseCLickListener;
import fun.sboy.panel.InfoPanel;
import fun.sboy.panel.OrderListPanel;
import fun.sboy.panel.SubAccountPanel;
import fun.sboy.pojo.ManagerUser;
import fun.sboy.util.IconUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class HomeView extends JFrame {

    public static ManagerUser user;
    private JPanel west = new JPanel(null);
    private JPanel center = new JPanel(new CardLayout());
    private JButton infoBtn = new JButton("卡号信息", new ImageIcon(IconUtil.load("message", 30, 30, 1)));
    private JButton orderBtn = new JButton("交易列表", new ImageIcon(IconUtil.load("order", 30, 30, 1)));
    private JButton subAccountBtn = new JButton("卡号转账", new ImageIcon(IconUtil.load("sub_account", 30, 30, 1)));
    private JButton exitBtn = new JButton("退出登录", new ImageIcon(IconUtil.loadSmall("go-to-link")));

    //面板
    private InfoPanel infoPanel = new InfoPanel(null);
    private OrderListPanel orderListPanel = new OrderListPanel(null);
    private SubAccountPanel subAccountPanel = new SubAccountPanel(null);

    public HomeView() {
        setSize(1000, 800);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        //初始化
        initialization();
        add(west, BorderLayout.WEST);
        center.add(infoPanel, "infoPanel");
        center.add(orderListPanel, "orderListPanel");
        center.add(subAccountPanel, "subAccountPanel");
        add(center, BorderLayout.CENTER);

        //添加布局等
        setIconImage(IconUtil.load("logo", 100, 100, 1));
        setTitle("银行管理系统——Home");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    private void initialization() {
        west.setPreferredSize(new Dimension(150, 800));
        west.setBackground(Color.lightGray);
        ArrayList<JButton> list = new ArrayList<>();
        list.add(infoBtn);
        list.add(orderBtn);
        list.add(subAccountBtn);
        list.add(exitBtn);

        //批量处理按钮
        west.setBackground(Color.lightGray);

        for (int i = 0; i < list.size(); i++) {
            JButton j = list.get(i);
            if (i != list.size() - 1) {
                j.setBounds(0, 50 * i, 150, 50);
            } else {
                j.setBounds(0, 800 - 85, 150, 50);
            }
            j.setContentAreaFilled(false);
            j.setBorder(BorderFactory.createEmptyBorder());
            west.add(j);
        }

        //为不同的导航按钮添加事件
        CardLayout card = (CardLayout) center.getLayout();
        infoBtn.addMouseListener(new MouseCLickListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                infoPanel.reload();
                card.show(center, "infoPanel");
                center.repaint();
                center.revalidate();
                setTitle("银行管理系统——Home");
            }
        });
        orderBtn.addMouseListener(new MouseCLickListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                orderListPanel.reload();
                center.repaint();
                center.revalidate();
                card.show(center, "orderListPanel");
                setTitle("银行管理系统——交易列表");
            }
        });
        subAccountBtn.addMouseListener(new MouseCLickListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                subAccountPanel.reload();
                center.repaint();
                center.revalidate();
                card.show(center, "subAccountPanel");
                setTitle("银行管理系统——转账");
            }
        });
        exitBtn.addMouseListener(new MouseCLickListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                center.repaint();
                center.revalidate();
                HomeView.user = null;
                dispose();
                new Main();
            }
        });

        west.add(infoBtn);
        west.add(orderBtn);
        west.add(subAccountBtn);
        west.add(exitBtn);
        west.setBackground(Color.lightGray);
    }

}
